﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using YesBlog.Framework.Entity;

namespace YesBlog.Infrastructure.Context
{
    public class YesDbContext : DbContext, IDbContext
    {
        public YesDbContext(DbContextOptions<YesDbContext> options) : base(options)
        {

        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            // 动态地加入实体
            var typesToRegister = Assembly.GetAssembly(typeof(EntityBase)).GetTypes().Where(type => !String.IsNullOrEmpty(type.Namespace)).Where(type => type.BaseType == typeof(EntityBase));
            foreach (var entityType in typesToRegister)
            {
                if (modelBuilder.Model.FindEntityType(entityType) != null)
                {
                    continue;
                }
                modelBuilder.Model.AddEntityType(entityType);
            }
        }
    }
}
